package fgame.resource.decoder
{
	import fgame.GameState;
	
	import flare.basic.Scene3D;
	import flare.core.Pivot3D;
	import flare.loaders.ZF3DLoader;
	import flare.system.Library3D;
	
	import flash.events.Event;
	
	import ricky.Interface.IDecoder;
	import ricky.Interface.ILoadComplete;
	import ricky.loader.Request;

	public class Zf3dDecoder  implements IDecoder
	{
		public function Zf3dDecoder()
		{
			library=new Library3D();
			library.addEventListener(Event.COMPLETE,onComplete);
		}

		public function doDecoder(req:Request, onComplete:ILoadComplete):void
		{
			var data:*=req.data;
			var loader:ZF3DLoader=new ZF3DLoader(data);
			loader.name=req.key;
			loader.userData={"req":req,"comp":onComplete};
			if ( !library.getItem( req.key ) ) {
				library.push( loader );
				library.addItem( req.key, loader );
			}
		}
		
		private function onComplete(e:Event):void
		{
			var user:Object,req:Request,onComp:ILoadComplete;
			for each(var item:ZF3DLoader in library.items)
			{
				if(item.loaded && item.userData)
				{
					user=item.userData;
					req=user["req"];
					onComp=user["comp"];
					req.data=item;
					item.userData=null;
					library.removeItem(req.key);
					onComp.doComplete(req);
				}
			}
			
//			Debug.traceMe(library.items.length,"library.items.length");
			
		}
		
		
		private var library:Library3D;

		
	}
}